//
//  WCTableHeaderView.m
//  WabbitCode
//
//  Created by William Towe on 11/22/08.
//  Copyright 2008 Revolution Software. All rights reserved.
//

#import "WCTableHeaderView.h"
#import "WCProject.h"


static NSString *WCTableHeaderViewMenuTitle = @"WCTableHeaderViewMenuTitle";

@implementation WCTableHeaderView
#pragma *** Subclass Overrides ***
- (void)awakeFromNib {
	NSMenu *menu = [[NSMenu alloc] initWithTitle:WCTableHeaderViewMenuTitle];
	
	for (NSTableColumn *column in [[self tableView] tableColumns]) {
		NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[column identifier] action:@selector(_toggleColumnHidden:) keyEquivalent:@""];
		[item setTarget:self];
		[menu addItem:item];
		[item release];
	}
	
	[self setMenu:menu];
	[menu setDelegate:self];
	[menu release];
}
#pragma mark NSMenu Delegate
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu {
	return [menu numberOfItems];
}

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel {
	if ([[[self tableView] tableColumnWithIdentifier:[item title]] isHidden])
		[item setState:NSOffState];
	else
		[item setState:NSOnState];
	return YES;
}
#pragma mark *** Private Methods ***
#pragma mark IBActions
- (IBAction)_toggleColumnHidden:(id)sender {
	NSTableColumn *column = [[self tableView] tableColumnWithIdentifier:[sender title]];
	[column setHidden:![column isHidden]];
	
	[[self tableView] sizeLastColumnToFit];
}
@end
